/** * 在Before中完成FileSystem的初始化操作 */ @Before publicvoidsetUp()throws Exception { URI uri = new URI("hdfs://bigdata01:9000"); Configuration configuration = new Configuration();
/* * @description: 获取HDFS上面的文件信息,包括文件的具体信息 */ @Test publicvoidlist_Files()throws Exception { RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(new Path("/hdfsapi/data/20201011"), true); while (files.hasNext()) { //LocatedFileStatus class defines a FileStatus that includes a file's block locations. LocatedFileStatus fileStatus = files.next(); //Get the path of the file in hdfs String path = fileStatus.getPath().toString(); //Get the block size of the file. long blockSize = fileStatus.getBlockSize(); //Get the length of this file, in bytes. long len = fileStatus.getLen(); //Get FsPermission associated with the file.default "rwxrwxrwx" FsPermission permission = fileStatus.getPermission(); //Is this a directory? @return true if this is a directory String isDir = fileStatus.isDirectory() ? "文件夹" : "文件"; //Get the file's block locations BlockLocation[] locations = fileStatus.getBlockLocations(); System.out.println("path:" + path); System.out.println("blockSize:" + blockSize); System.out.println("len:" + len); System.out.println("permission:" + permission); System.out.println("isDir:" + isDir); /** * Represents the network location of a block, information about the hosts * that contain block replicas, and other block metadata (E.g. the file * offset associated with the block, length, whether it is corrupt, etc). */ for (BlockLocation location : locations) { String[] hosts = location.getHosts(); for (String host : hosts) { System.out.println("HOST: " + host + " ......................."); } } } }